package com.cennavi.minemap.tool;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.cennavi.minemap.util.Constants;
import com.alibaba.fastjson.JSONObject;
import com.cennavi.minemap.dao.UploadDao2;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;

public class ParseCsvFile implements ParseFile {

	GeometryFactory factory = new GeometryFactory();

	@Override
	public Map<String, Integer> parse(String fullPath, UploadDao2 uploadDao, int type, int userId, int uploadId) {
		// TODO Auto-generated method stub

		File f = new File(fullPath);
		InputStreamReader read = null;

		try {
			read = new InputStreamReader(new FileInputStream(f), "UTF-8");
		} catch (UnsupportedEncodingException | FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		BufferedReader bufferedReader = new BufferedReader(read);

		Map<String, Integer> rMap = new HashMap<>();

		String line;
		List<Map<String, Object>> list = new ArrayList<>();
		int succLine = 0;
		int fileLine = 0;
		int i = 0;
		Pattern pattern = Pattern.compile("(,)?((\"[^\"]*(\"{2})*[^\"]*\")*[^,]*)");
		List<String> head = new ArrayList<String>();
		try {
			// uploadDao.createUploadDetail(userId, uploadId);
			while ((line = bufferedReader.readLine()) != null) {
				try {
					Matcher matcher = pattern.matcher(line);
					int j = 0;

					JSONObject json = new JSONObject();
					Map<String, Object> map = new HashMap<>();
					while (matcher.find()) {
						String cell = matcher.group(2);
						Pattern pattern2 = Pattern.compile("\"((.)*)\"");
						Matcher matcher2 = pattern2.matcher(cell);
						if (matcher2.find()) {
							cell = matcher2.group(1);
							if (i == 0) {
								head.add(cell);
							} else {
								if (!head.get(j).toUpperCase().equals(Constants.USER_UPLOAD_CSV_GEO_HEAD))
									json.put(head.get(j), cell);
								else {
									Geometry geometry = null;
									if (type == Constants.USER_UPLOAD_TYPE_POINT) {
										String coord[] = cell.trim().split(",");
										Coordinate coordinate = new Coordinate(Double.parseDouble(coord[0]),
												Double.parseDouble(coord[1]));
										geometry = factory.createPoint(coordinate);
									}
									if (type == Constants.USER_UPLOAD_TYPE_LINE) {

									}
									if (type == Constants.USER_UPLOAD_TYPE_FACE) {

									}
									map.put("geom", geometry.toString());
								}
							}
						}
						j++;
					}
					if (i != 0) {
						fileLine++;
						map.put("attr", json.toJSONString());
						map.put("uploadId", uploadId);
						list.add(map);
					}
					if (list.size() == 1000) {
						succLine = succLine + list.size();
						uploadDao.addUploadDetail(list, userId, uploadId);
						list.clear();
					}
					i++;
				} catch (Exception e) {
					continue;
				}

			}
		} catch (NumberFormatException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (list.size() > 0) {
			succLine = succLine + list.size();
			uploadDao.addUploadDetail(list, userId, uploadId);
		}
		rMap.put("fileLine", fileLine);
		rMap.put("succLine", succLine);

		try {
			read.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return rMap;

	}

	// public static void main(String[] args) throws FileNotFoundException {
	// ParseFile parse = new ParseCsvFile();
	// parse.parse("F:\\csv_template.csv", new UploadDao2(), 1, 1, 1);
	// }
}
